package com.eascs.web.o2o.web.reward.service.impl;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.o2o.web.reward.dao.BaseDao;
import com.eascs.web.o2o.web.reward.entity.ProvinceOverdueQueryForm;
import com.eascs.web.o2o.web.reward.entity.OverdualForm;
import com.eascs.web.o2o.web.reward.entity.ProvinceOverdue;
import com.eascs.web.o2o.web.reward.service.ProvinceOverdueService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

@Service
public class ProvinceOverdueServiceImpl implements ProvinceOverdueService {
	
	private static Logger log = LoggerFactory.getLogger(ProvinceOverdueServiceImpl.class);

	@Inject
	BaseDao baseDao;
	

	// 根据平台查询省区逾期率报表
	@Override
	public PageResult<Object[]> getProvinceOverdueDetail(ProvinceOverdueQueryForm entity) {
		
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getShowPage());
		StringBuffer querySql = new StringBuffer();
		querySql.append(baseDao.getSql("sql.ProvinceOverdueServiceImpl.getProvinceOverdueDetail")); 
		querySql.append(" AND t.t_month = ?");
		
		sqlPageQuery.addParam(entity.getMonth());
		
		if(!StringUtil.isEmpty(entity.getProvince())){
			querySql.append(" AND t.province_name = ?");
			sqlPageQuery.addParam(entity.getProvince());
		}
	
		sqlPageQuery.setQuerySql(querySql.toString());
		return baseDao.findDataWithPage(sqlPageQuery);
	}

	
	
	@Override
	public PageResult<Object[]> downProvinceOverdueDetail(ProvinceOverdueQueryForm entity) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getShowPage());
		StringBuffer querySql = new StringBuffer();
		querySql.append(baseDao.getSql("sql.provinceOverdueServcieImpl.downProvinceOverdueDetail")); 
		sqlPageQuery.setQuerySql(querySql.toString());
		return baseDao.findDataWithPage(sqlPageQuery);
	}
	
	
	
	@Override
	@Transactional("transactionManagerDbForReward")
	public BaseRestResult<String> addProvinceOverdueList(List<List<Object>> result){
			BaseRestResult<String> results = BaseRestResult.info("导入成功").andRefresh();
			User user=UserHolder.getUser();
			//删除当前月份前一个月的所有数据
			baseDao.executeSql(" delete from dm_reward_province_overdue_ratio where  t_month = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month),'%Y-%m')");
			//将excel中的数据导入到数据库中
			if(result!=null&&result.size()>0){
				for (int i = 0; i < result.size(); i++) {
					@SuppressWarnings({ "unchecked", "rawtypes" })
					List<Object> list = (List) result.get(i);
					ProvinceOverdue provinceOverduePO = new ProvinceOverdue();
					provinceOverduePO.setT_month(list.get(0).toString());
					provinceOverduePO.setProvince_code(list.get(1)==null?null:list.get(1).toString());
					provinceOverduePO.setProvince_name(list.get(2)==null?null:list.get(2).toString());	
					provinceOverduePO.setBill_code(list.get(3)==null?null:list.get(3).toString());	
					provinceOverduePO.setLoaner_name(list.get(4)==null?null:list.get(4).toString());	
					provinceOverduePO.setUser_no(list.get(5)==null?null:list.get(5).toString());
					provinceOverduePO.setUser_code(list.get(6)==null?null:list.get(6).toString());
					provinceOverduePO.setUser_name(list.get(7)==null?null:list.get(7).toString());			
					provinceOverduePO.setLtd(list.get(8)==null?null:list.get(8).toString());
					provinceOverduePO.setLtd_code(list.get(9)==null?null:list.get(9).toString());
					provinceOverduePO.setLtd_name(list.get(10)==null?null:list.get(10).toString());					
					provinceOverduePO.setLoan_amount((BigDecimal)list.get(11));					
					provinceOverduePO.setLoan_date((Date)list.get(12));					
					provinceOverduePO.setExpire_date((Date)list.get(13));
					provinceOverduePO.setAcc_overdue_interest((BigDecimal)list.get(14));					
					provinceOverduePO.setAcc_receive_overdue_interest((BigDecimal)list.get(15));
					provinceOverduePO.setAcc_overdue_principal((BigDecimal)list.get(16));
					provinceOverduePO.setIs_lock("N");
					provinceOverduePO.setCreate_user(user.getCname());
					provinceOverduePO.setCreate_date(new Date());
					provinceOverduePO.setUpdate_user(user.getCname());
					provinceOverduePO.setUpdate_date(new Date());
					provinceOverduePO.setConfirm_user(user.getCname());
					provinceOverduePO.setConfirm_date(new Date());
					baseDao.saveEntity(provinceOverduePO);
			    	}				
			}else{
				log.error("导入的数据为空，事务回滚");
				throw new RuntimeException("导入的数据为空，请重新导入！");
			}
		   return results;
	}


}
